using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models.Fund.Deferred;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Services.Fund.Deferred
{
    /// <summary>
    /// 资金服务类
    /// </summary>
    public class FundService : BaseService
    {
        /// <summary>
        /// 创建资金服务类
        /// </summary>
        /// <param name="config">配置对象</param>
        public FundService(Config config) : base(config)
        {
        }

        /// <summary>
        /// 获取API URL
        /// </summary>
        /// <param name="requestClass">请求类名</param>
        /// <param name="apiPath">接口路径</param>
        /// <returns>完整的API URL</returns>
        private string GetApiUrl(Type requestClass, string? apiPath = null)
        {
            if (!string.IsNullOrEmpty(apiPath))
            {
                return apiPath;
            }

            // 使用请求类中定义的ApiPrefix和ApiUri常量
            var apiPrefixField = requestClass.GetField("ApiPrefix");
            var apiUriField = requestClass.GetField("ApiUri");
            
            if (apiPrefixField != null && apiUriField != null)
            {
                string apiPrefix = (string)apiPrefixField.GetValue(null)!;
                string apiUri = (string)apiUriField.GetValue(null)!;
                
                // 组合API路径
                string path = apiPrefix.TrimStart('/') + "/" + apiUri.TrimStart('/');
                return "/" + path;
            }

            // 如果没有定义ApiPrefix和ApiUri，使用默认路径
            return "/open/fund/deferred/activation";
        }

        /// <summary>
        /// 查询资金管控状态
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<DeferredActivationQueryResponse> QueryDeferredActivationAsync(DeferredActivationQueryRequest request)
        {
            string apiPath = GetApiUrl(typeof(DeferredActivationQueryRequest));
            return await PostAsync<DeferredActivationQueryRequest, DeferredActivationQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 开通资金管控
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<DeferredActivationOpenResponse> OpenDeferredActivationAsync(DeferredActivationOpenRequest request)
        {
            string apiPath = GetApiUrl(typeof(DeferredActivationOpenRequest));
            return await PostAsync<DeferredActivationOpenRequest, DeferredActivationOpenResponse>(request, apiPath);
        }

        /// <summary>
        /// 关停资金管控
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<DeferredActivationCloseResponse> CloseDeferredActivationAsync(DeferredActivationCloseRequest request)
        {
            string apiPath = GetApiUrl(typeof(DeferredActivationCloseRequest));
            return await PostAsync<DeferredActivationCloseRequest, DeferredActivationCloseResponse>(request, apiPath);
        }

        /// <summary>
        /// 查询资金流水
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<FundFlowQueryResponse> QueryFundFlowAsync(FundFlowQueryRequest request)
        {
            string apiPath = GetApiUrl(typeof(FundFlowQueryRequest));
            return await PostAsync<FundFlowQueryRequest, FundFlowQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 冻结资金
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<FundFreezeResponse> FreezeFundAsync(FundFreezeRequest request)
        {
            string apiPath = GetApiUrl(typeof(FundFreezeRequest));
            return await PostAsync<FundFreezeRequest, FundFreezeResponse>(request, apiPath);
        }

        /// <summary>
        /// 解冻资金
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<FundUnfreezeResponse> UnfreezeFundAsync(FundUnfreezeRequest request)
        {
            string apiPath = GetApiUrl(typeof(FundUnfreezeRequest));
            return await PostAsync<FundUnfreezeRequest, FundUnfreezeResponse>(request, apiPath);
        }

        /// <summary>
        /// 续冻资金
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<FundRefreezeResponse> RefreezeFundAsync(FundRefreezeRequest request)
        {
            string apiPath = GetApiUrl(typeof(FundRefreezeRequest));
            return await PostAsync<FundRefreezeRequest, FundRefreezeResponse>(request, apiPath);
        }
    }
} 